﻿Imports System.IO
Imports System.Windows.Forms

Public Class CharacterRace
    Private _Id As Integer = -1
    Private _name As String
    Private _desc As String
    Private _strMod As Integer
    Private _dexMod As Integer
    Private _intMod As Integer
    Private _unknowSexImage As String
    Private _maleImage As String
    Private _femaleImage As String
    Private _malePortrait(5) As String
    Private _femalePortrait(5) As String

    Public Property RaceID() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return _desc
        End Get
        Set(ByVal value As String)
            _desc = value
        End Set
    End Property

    Public Property StrengthModifier() As Integer
        Get
            Return _strMod
        End Get
        Set(ByVal value As Integer)
            _strMod = value
        End Set
    End Property

    Public Property DexterityModifier() As Integer
        Get
            Return _dexMod
        End Get
        Set(ByVal value As Integer)
            _dexMod = value
        End Set
    End Property

    Public Property IntelligenceModifier() As Integer
        Get
            Return _intMod
        End Get
        Set(ByVal value As Integer)
            _intMod = value
        End Set
    End Property

    Public Property UnknownSexImage() As String
        Get
            Return _unknowSexImage
        End Get
        Set(ByVal value As String)
            _unknowSexImage = value
        End Set
    End Property

    Public Property MaleImage() As String
        Get
            Return _maleImage
        End Get
        Set(ByVal value As String)
            _maleImage = value
        End Set
    End Property

    Public Property FemaleImage() As String
        Get
            Return _femaleImage
        End Get
        Set(ByVal value As String)
            _femaleImage = value
        End Set
    End Property

    Public Property MalePortrait(ByVal num As Integer) As String
        Get
            If num >= 0 And num <= 5 Then
                Return _malePortrait(num)
            Else
                Return ""
            End If
        End Get
        Set(ByVal value As String)
            If num >= 0 And num <= 5 Then
                _malePortrait(num) = value
            End If
        End Set
    End Property

    Public Property FemalePortrait(ByVal num As Integer) As String
        Get
            If num >= 0 And num <= 5 Then
                Return _femalePortrait(num)
            Else
                Return ""
            End If
        End Get
        Set(ByVal value As String)
            If num >= 0 And num <= 5 Then
                _femalePortrait(num) = value
            End If
        End Set
    End Property

    Public Function ToXElement() As XElement
        Dim raceXML = <race ID=<%= _Id %>>
                          <name><%= _name %></name>
                          <desc><%= _desc %></desc>
                          <strMod><%= _strMod %></strMod>
                          <dexMod><%= _dexMod %></dexMod>
                          <intMod><%= _intMod %></intMod>
                          <unknowSexPic><%= _unknowSexImage %></unknowSexPic>
                          <maleSexPic><%= _maleImage %></maleSexPic>
                          <femaleSexPic><%= _femaleImage %></femaleSexPic>
                          <portraits>
                              <male>
                                  <%= From pic In _malePortrait _
                                      Select <pic><%= pic %></pic> %>
                              </male>
                              <female>
                                  <%= From pic In _femalePortrait _
                                      Select <pic><%= pic %></pic> %>
                              </female>
                          </portraits>
                      </race>
        Return raceXML
    End Function

    Public Sub LoadXML(ByVal raceXML As String)
        Dim sr As New StringReader(raceXML)
        Dim raceX As XElement

        Try
            raceX = XElement.Load(sr)
        Catch ex As Exception
            Failed(raceXML, ex.ToString)
            Exit Sub
        End Try
        Try
            _Id = Integer.Parse(raceX.@ID)
            _name = raceX.<name>.Value
            _desc = raceX.<desc>.Value
            If Integer.TryParse(raceX.<strMod>.Value, _strMod) = False Then
                Failed(raceXML, "strMod invalid integer")
                Exit Sub
            End If
            If Integer.TryParse(raceX.<dexMod>.Value, _dexMod) = False Then
                Failed(raceXML, "dexMod invalid integer")
                Exit Sub
            End If
            If Integer.TryParse(raceX.<intMod>.Value, _intMod) = False Then
                Failed(raceXML, "intMod invalid integer")
                Exit Sub
            End If
            _unknowSexImage = raceX.<unknowSexPic>.Value
            _maleImage = raceX.<maleSexPic>.Value
            _femaleImage = raceX.<femaleSexPic>.Value
            Dim portraitPics = raceX...<portraits>...<male>...<pic>
            If portraitPics.Count <> 6 Then
                Failed(raceXML, "Invalid Male Portrait Count")
                Exit Sub
            End If
            _malePortrait(0) = portraitPics(0).Value
            _malePortrait(1) = portraitPics(1).Value
            _malePortrait(2) = portraitPics(2).Value
            _malePortrait(3) = portraitPics(3).Value
            _malePortrait(4) = portraitPics(4).Value
            _malePortrait(5) = portraitPics(5).Value
            portraitPics = raceX...<portraits>...<female>...<pic>
            If portraitPics.Count <> 6 Then
                Failed(raceXML, "Invalid Female Portrait Count")
                Exit Sub
            End If
            _femalePortrait(0) = portraitPics(0).Value
            _femalePortrait(1) = portraitPics(1).Value
            _femalePortrait(2) = portraitPics(2).Value
            _femalePortrait(3) = portraitPics(3).Value
            _femalePortrait(4) = portraitPics(4).Value
            _femalePortrait(5) = portraitPics(5).Value
        Catch ex As Exception
            Failed(raceXML, ex.ToString)
        End Try
        
    End Sub

    Private Sub Failed(ByVal xmlString As String, ByVal ex As String)
        MessageBox.Show(xmlString + vbCrLf + vbCrLf + ex, "Failed to LoadXML", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Public ReadOnly Property IsValid() As Boolean
        Get
            Return CheckValidData()
        End Get
    End Property

    Private Function CheckValidData() As Boolean
        ' Check main details
        If _name = "" Or Description = "" Then
            Return False
        End If

        ' Check sex images
        If _unknowSexImage = "" Or _maleImage = "" Or _femaleImage = "" Then
            Return False
        End If

        ' Check portraits
        For lp As Integer = 0 To 5
            If _malePortrait(lp) = "" Or _femalePortrait(lp) = "" Then
                Return False
            End If
        Next
        Return True
    End Function
End Class
